package com.example.owner.lettery1.other;

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.util.Linkify;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/3.
 */
public class TextTableViewHolder {
    private ViewGroup mRowLayout;
    private List<View> mViews;
    private TextView extraView;

    public TextTableViewHolder(ViewGroup rowLayout) {
        mRowLayout = rowLayout;
        mViews = new ArrayList<>();
        mRowLayout.setTag(this);
        extraView = (TextView) mRowLayout.getChildAt(0);
    }

    public void showExtra(boolean isShow) {
        if (isShow) {
            extraView.setVisibility(View.VISIBLE);
        } else {
            extraView.setVisibility(View.GONE);
        }
    }

    public void addItem(View view) {
        mViews.add(view);
    }

    public ViewGroup getRowLayout() {
        return mRowLayout;
    }

    public int size() {
        return mViews.size();
    }


    /**
     * 通过column获取控件
     *
     * @param column
     * @return
     */
    public <T extends View> T getView(int column) {
        return (T) mViews.get(column);
    }

    /****
     * 以下为辅助方法
     *****/

    public TextTableViewHolder setTexts(String... texts) {
        for (int i = 0; i < mViews.size(); i++) {
            TextView view = (TextView) mViews.get(i);
            view.setText(texts[i]);
        }
        return this;
    }
    public TextTableViewHolder setTextStyle(@NotNull Integer[] colors, @NotNull Float[] sizes, @NotNull Integer[] backgrounds) {
        for (int i = 0; i < mViews.size(); i++) {
            TextView view = (TextView) mViews.get(i);
            view.setTextColor(colors[i]);
            view.setTextSize(sizes[i]);
            view.setBackgroundResource(backgrounds[i]);
        }
        return this;

    }
    /**
     * 设置TextView的值
     * @param column
     * @param text
     * @return
     */
    public TextTableViewHolder setText(int column, String text) {
        TextView tv = getView(column);
        tv.setText(text);
        return this;
    }

    public TextTableViewHolder setBackgroundColor(int column, int color) {
        View view = getView(column);
        view.setBackgroundColor(color);
        return this;
    }

    public TextTableViewHolder setBackgroundRes(int column, int backgroundRes) {
        View view = getView(column);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    public TextTableViewHolder setTextColor(int column, int textColor) {
        TextView view = getView(column);
        view.setTextColor(textColor);
        return this;
    }

    public TextTableViewHolder setTextSize(int column, float textSize) {
        TextView view = getView(column);
        view.setTextSize(textSize);
        return this;
    }


    public TextTableViewHolder setVisible(int column, boolean visible) {
        View view = getView(column);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public TextTableViewHolder linkify(int column) {
        TextView view = getView(column);
        Linkify.addLinks(view, Linkify.ALL);
        return this;
    }

    public TextTableViewHolder setTypeface(Typeface typeface, int... columns) {
        for (int column : columns) {
            TextView view = getView(column);
            view.setTypeface(typeface);
            view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }

    public TextTableViewHolder setTag(int column, Object tag) {
        View view = getView(column);
        view.setTag(tag);
        return this;
    }

    public TextTableViewHolder setTag(int column, int key, Object tag) {
        View view = getView(column);
        view.setTag(key, tag);
        return this;
    }

    /**
     * 关于事件的
     */
    public TextTableViewHolder setOnClickListener(int column,
                                                  View.OnClickListener listener) {
        View view = getView(column);
        view.setOnClickListener(listener);
        return this;
    }

    public TextTableViewHolder setOnTouchListener(int column,
                                                  View.OnTouchListener listener) {
        View view = getView(column);
        view.setOnTouchListener(listener);
        return this;
    }

    public TextTableViewHolder setOnLongClickListener(int column,
                                                      View.OnLongClickListener listener) {
        View view = getView(column);
        view.setOnLongClickListener(listener);
        return this;
    }


}
